﻿using System;
using OpenCvSharp;
namespace UnitTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var template = Cv2.ImRead("template.bmp");
            var target = Cv2.ImRead("test/2.bmp");
            Cv2.Resize(target,target,new Size(1224,1024));
            // Cv2.FloodFill(target, new Point(300, 300), Scalar.White);
            // var w = target.Width;
            // var h = target.Height;
            // var mask = new Mat(new Size(w + 2, h + 2), MatType.CV_8UC1);
            //
            // Cv2.FloodFill(target, mask, new Point(100, 100), new Scalar(255));
            // Cv2.ImShow("FloodFill" , target);

            var rect = Cv2.SelectROI(target);
            var mask = new Mat();
            Mat bgdModel = new Mat();
            Mat fgdModel = new Mat();
            var newimag =new Mat(target,rect);
            rect = new Rect(0, 0, newimag.Width-1, newimag.Height-1);
            Cv2.GrabCut(newimag,mask,rect,bgdModel,fgdModel,12,GrabCutModes.InitWithRect);
            
            Mat result = new Mat(mask.Rows, mask.Cols, MatType.CV_8UC1);
            Cv2.ImShow("mask",mask);
            for (int i = 0; i < mask.Width; i++)
            {
                for (int j = 0; j < mask.Height; j++)
                {
                    byte v = mask.Get<byte>(j, i);
                    switch (v)
                    {
                        case 0:
                            result.Set<byte>(j, i, 0);
                            break;
                        case 1:
                            result.Set<byte>(j, i, 255);
                            break;
                        case 2:
                            result.Set<byte>(j, i, 50);
                            break;
                        case 3:
                            result.Set<byte>(j, i, 200);
                            break;
                    }
                }
            }
            Cv2.ImShow("result",result);

            // Mat result = new Mat();
           
            Cv2.WaitKey(0);
        }
    }
}